/*
    CETURY_LINK
		
    (C)2013 Semtech

Description: apps 

License: Revised BSD License, see LICENSE.TXT file include in the project

Maintainer: cheng yong
*/
#include "api_flash.h"
#include "platform.h"
#include "Comissioning.h"
#include <math.h>

u8 VT_FLAG = 0;
CAP_AFE cap_structure;

uint8_t CapSensorSampFlag = false;
uint8_t CapDataTxFlag = false;
uint8_t CapDataRxFlag = false;
uint8_t CapDataParaConfigFlag = false;
uint8_t CapValueState = false;
uint8_t CapCfgData[20];
uint8_t CapCfgDataLen = 0;

uint16_t CapDataLast = 0;

#define SET_VT_GPIO_15

#if defined SET_VT_GPIO_15
	VT_CFG vt_structure = {VT_CH6,VT_GPIO_15};
#elif defined SET_VT_GPIO_12
	VT_CFG vt_structure={VT_CH3,VT_GPIO_12};
#elif defined SET_VT_GPIO_3
	VT_CFG vt_structure={VT_CH2,VT_GPIO_3};
#elif defined SET_VT_GPIO_2
	VT_CFG vt_structure={VT_CH5,VT_GPIO_2};
#elif defined SET_VT_GPIO_0
	VT_CFG vt_structure={VT_CH1,VT_GPIO_0};
#endif
	
void CapApps_Init( void )
{
	cap_structure.I_DRIVE = DRIVE_I_400UA;	//Select 400uA driving current
	cap_structure.clk = In_CLK;	//Using CAR AFE internal clock	
	cap_structure.RCNT = RCNT_D;	//Set sampling time
	cap_structure.avg = 20;			// 20 times avg  记得改对应的宏定义#define AVG_COUNT
	cap_structure.Measuring_channel = CH0;	//Channel 0 is the measurement channel
	cap_structure.Reference_channel = CH1;	//Channel 1 is the Reference channel
	cap_structure.VT_CH = vt_structure;
	
	Conf_FreDiv(&cap_structure);
	Cap_Afe_Init(&cap_structure);	//Initialize CAP AFE
	
#if defined( VT_EN )
	VT_Init(&cap_structure);
#else
	OW_GPIOInit();
#endif	
	SetConfig(CFG_MPS_Single,CFG_Repeatbility_High);
	ConvertTempBreak();
}

void CapApps_paraDataCfgProc( void )
{
		uint8_t dataVialdLen = 0;
		uint16_t alarmThr =0,recoverThr=0,calibValue =0;
		uint8_t flashSaveFlag = false;
		uint8_t checksum = 0;
	  uint8_t  txDataBuffer[10],txDataLen = 0;
	  uint16_t tempValue = 0;
		uint16_t timeout = 300;
		
	  Uart1_McuPutBuffer(CapCfgData,CapCfgDataLen);
		while( !CapDataRxFlag )
		{
			Delay_ms(10);
			if( --timeout == 0 )
			{
				return;
			}
		}
		CapDataRxFlag = false;
		dataVialdLen = CapCfgData[1] + 2;
//		checksum = CRC8_MAXIM(inData,dataVialdLen);
//		if( checksum != inData[dataVialdLen])
//		{
//			return;
//		}
    if( CapCfgData[2] == 0x01 )
		{
			alarmThr = CapCfgData[3];
			alarmThr <<= 8;
			alarmThr |= CapCfgData[4];
			recoverThr = CapCfgData[5];
			recoverThr <<= 8;
			recoverThr |= CapCfgData[6];
			calibValue = CapCfgData[7];
			calibValue <<= 8;
			calibValue |= CapCfgData[8];
			if( alarmThr != AppCfgPara.ws11AlarmThr )
			{
				AppCfgPara.ws11AlarmThr = alarmThr;
				flashSaveFlag = true;
			}
			if( alarmThr != AppCfgPara.ws11RecoverThr )
			{
				AppCfgPara.ws11RecoverThr = recoverThr;
				flashSaveFlag = true;
			}
			if( alarmThr != AppCfgPara.ws11CalibValue )
			{
				AppCfgPara.ws11CalibValue = calibValue;
				flashSaveFlag = true;
			}
			if( flashSaveFlag == true )
			{
				EepromBaseParametersUpdate();
			}
			txDataBuffer[0] = 0x5A;
			txDataBuffer[1] = 0x01;
			txDataBuffer[2] = 0x80;
			txDataBuffer[3] = 0x80;
			txDataBuffer[4] = CRC8_MAXIM(txDataBuffer,8);
			txDataLen = 5;
		}
		else
		{
			txDataBuffer[0] = 0x5A;
			txDataBuffer[1] = 0x06;
			tempValue = (int16_t)(AppCfgPara.ws11AlarmThr );
			txDataBuffer[2] = (tempValue >> 8)&0xff;
			txDataBuffer[3] = (tempValue >> 0)&0xff;
			tempValue = (uint16_t)(AppCfgPara.ws11RecoverThr);
			txDataBuffer[4] = (tempValue >> 8)&0xff;
			txDataBuffer[5] = (tempValue >> 0)&0xff;
			tempValue = (int16_t)(AppCfgPara.ws11CalibValue);
			txDataBuffer[6] = (tempValue >> 8)&0xff;
			txDataBuffer[7] = (tempValue >> 0)&0xff;
			txDataBuffer[8] = CRC8_MAXIM(txDataBuffer,8);
			txDataLen = 9;
		}
		Uart1_McuPutBuffer( txDataBuffer, txDataLen );
}

void CapApps_RxDataProc( uint8_t *inData,uint8_t dataLen )
{
		if( inData[0] == 0xA5 )
		{
			if( dataLen <= 20 )
			{
				memcpy(CapCfgData,inData,dataLen);
				CapDataRxFlag = true;
			}
		}
		else
		{
			if( dataLen > 2 )
			{
				if( dataLen > 4 )
				{
					memcpy(CapCfgData,inData,dataLen);
					CapCfgDataLen = dataLen;
					CapDataParaConfigFlag = true;
				}
				else
				{
					CapDataTxFlag = true;
				}
			}
		}
}

uint8_t CapApps_CapDataAnalysis( float capValue )
{
	int16_t capValue1000 = 0;
	int16_t capValueDiv = 0;
	
	capValue1000 = capValue*1000;
	
	capValueDiv = capValue1000 - CapDataLast;
	
	CapDataLast = capValue1000;
	
	if( capValueDiv < 0 )
	{
		capValueDiv = 0-capValueDiv;
	}
	if( capValueDiv > 100 )
	{
		return true;
	}
	else if( AppCfgPara.ws11CalibValue != 0 )
	{
		capValue1000 = (capValue1000 - AppCfgPara.ws11CalibValue);
		if( capValue1000 < 0 )
		{
			capValue1000 = 0 - capValue1000;
		}
		
		if( CapValueState == false )
		{
			if( capValue1000 > AppCfgPara.ws11AlarmThr )
			{
				CapValueState = true;
				return true;
			}
		}
		else if( CapValueState == true )
		{
			if( capValue1000 < AppCfgPara.ws11AlarmThr )
			{
				CapValueState = false;
				return true;
			}
		}
  }
	
	return false;
}

void CapApps_CapDataSampProc( void )
{
	static float preCapValue = 0;		//记录上一次采集的电容值
		uint16_t tempValue = 0;
		uint8_t  txDataBuffer[10];
	  uint16_t  timeout = 100;
		float tempratureOW = 0;
	  
	  Uart1_RxGPIOIntDisable();
		Cap_Afe_Init(&cap_structure);	//Initialize CAP AFE
		
#if defined VT_EN			
		GetVT(&cap_structure,50);	//Measuring temperature information
		tempratureVT = -507.8*cap_structure.VT + 341.6;
#else
		GetTemp(&tempratureOW);
#endif
		Single_Measure(&cap_structure);		//采集一次
		cap_structure.cap = Cref * (cap_structure.R_F/cap_structure.M_F*cap_structure.coef)  - Cp;
		if (fabs(cap_structure.cap - preCapValue) > 0.1f) {
			// 差值大于0.1f，再采集一次
			Single_Measure(&cap_structure);
			cap_structure.cap = Cref * (cap_structure.R_F/cap_structure.M_F*cap_structure.coef)  - Cp;
			// 不论第二次采集与上一次的差值是否大于0.1f，都用第二次采集值
//			if (fabs(cap_structure.cap - preCapValue) > 0.1f)
//			{
//				
//			}
			
		}

		preCapValue = cap_structure.cap;
		if( CapApps_CapDataAnalysis( cap_structure.cap ) == true ) //电容值告警判断
		{
			if( CapDataTxFlag != true && CapDataParaConfigFlag != true )
			{
				txDataBuffer[0] = 0xAA;
				txDataBuffer[1] = 0xAA;
				Uart1_McuPutBuffer( txDataBuffer, 2 );//唤醒主机
				while( !CapDataTxFlag )
				{
					Delay_ms(10);
					if( --timeout == 0 )
					{
						break;
					}
				}
			}
		}
	  if( CapDataTxFlag == true )
		{
			
			if (tempratureOW >= 25 && tempratureOW < 45) {
				tempValue = (uint16_t)(cap_structure.cap * 1000) - (tempratureOW -25)*20.5;
			} else if (tempratureOW >= 45) {
				tempValue = (uint16_t)(cap_structure.cap * 1000) - 20*20.5;
				tempValue = tempValue - (tempratureOW - 45) * 21.5;
			} else if (tempratureOW >=0 && tempratureOW < 25) {
				tempValue = (uint16_t)(cap_structure.cap *1000) + (25 - tempratureOW)*17.5;
			} else if (tempratureOW < 0 && tempratureOW >= -1.8) {
				tempValue = (uint16_t)(cap_structure.cap *1000) + (25 - tempratureOW )*19.0;
			} else if (tempratureOW < -1.8) {
				tempValue = (uint16_t)(cap_structure.cap *1000) + (25 - tempratureOW)*23.0 + 30;
			}
			
//			tempValue = (int16_t)(cap_structure.cap *1000);
			txDataBuffer[0] = (tempValue >> 8)&0xff;
			txDataBuffer[1] = (tempValue >> 0)&0xff;
			tempValue = (int16_t)(cap_structure.R_F *10);
			txDataBuffer[2] = (tempValue >> 8)&0xff;
			txDataBuffer[3] = (tempValue >> 0)&0xff;
			tempValue = (uint16_t)(cap_structure.M_F *1000);
			txDataBuffer[4] = (tempValue >> 8)&0xff;
			txDataBuffer[5] = (tempValue >> 0)&0xff;
			tempValue = (int16_t)(tempratureOW *10);
			txDataBuffer[6] = (tempValue >> 8)&0xff;
			txDataBuffer[7] = (tempValue >> 0)&0xff;
			txDataBuffer[8] = CRC8_MAXIM(txDataBuffer,8);
			
			Uart1_McuPutBuffer( txDataBuffer, 9 );
			
			CapDataTxFlag = false;
		}
}
	
	
